/** 
 * Copyright (C), 2010-2012, TIETIAO Software Co.,Ltd. ALL RIGHTS RESERVED. 
 * File name:LetterAction.java
 * Version:1.0
 * Date:2011-12-26
 * Description: 私信Action
 * author:zhijian.zhang
 */
package com.tietiao.action.letter;

import java.io.PrintWriter;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.tietiao.common.action.BaseAction;
import com.tietiao.common.util.DateFormater;
import com.tietiao.common.util.PropertyUtil;
import com.tietiao.common.util.StringUtil;
import com.tietiao.dao.model.PrivateLetter;
import com.tietiao.dao.model.SysUser;
import com.tietiao.service.privateletter.PrivateLetterService;
import com.tietiao.service.sysuser.SysUserService;

@SuppressWarnings("serial")
@Controller
@Scope("prototype")
public class LetterAction extends BaseAction {

	@Resource
	private PrivateLetterService privateLetterService;
	@Resource
	private SysUserService sysUserService;

	private List<PrivateLetter> privateLetters;
	// 好友ID
	private Integer friendUserId;
	// 私信ID
	private Integer letterId;
	// 私信内容
	private String content;

	private SysUser friendUser;
	// 好友名称
	private String friendUserName;

	public String getFriendUserName() {
		return friendUserName;
	}

	public void setFriendUserName(String friendUserName) {
		this.friendUserName = friendUserName;
	}

	/**
	 * @return the friendUser
	 */
	public SysUser getFriendUser() {
		return friendUser;
	}

	/**
	 * @param friendUser
	 *            the friendUser to set
	 */
	public void setFriendUser(SysUser friendUser) {
		this.friendUser = friendUser;
	}

	/**
	 * @return the letterId
	 */
	public Integer getLetterId() {
		return letterId;
	}

	/**
	 * @param letterId
	 *            the letterId to set
	 */
	public void setLetterId(Integer letterId) {
		this.letterId = letterId;
	}

	/**
	 * @return the content
	 */
	public String getContent() {
		return content;
	}

	/**
	 * @param content
	 *            the content to set
	 */
	public void setContent(String content) {
		this.content = content;
	}

	/**
	 * @return the privateLetters
	 */
	public List<PrivateLetter> getPrivateLetters() {
		return privateLetters;
	}

	/**
	 * @param privateLetters
	 *            the privateLetters to set
	 */
	public void setPrivateLetters(List<PrivateLetter> privateLetters) {
		this.privateLetters = privateLetters;
	}

	/**
	 * @return the friendUserId
	 */
	public Integer getFriendUserId() {
		return friendUserId;
	}

	/**
	 * @param friendUserId
	 *            the friendUserId to set
	 */
	public void setFriendUserId(Integer friendUserId) {
		privateLetters = privateLetterService.loadPrivateLetters(getUser()
				.getUserId());
		// 拆分好友昵称及头像
		if (privateLetters != null && privateLetters.size() > 0) {
			String relPhotoPath = PropertyUtil.getValue("PIC_SERVER");
			for (PrivateLetter letter : privateLetters) {
				String[] nameAndPhoto = letter.getFriendPetNameAndPhoto()
						.split("####");
				letter.setFriendPetName(nameAndPhoto[0]);
				if (nameAndPhoto.length == 2
						&& StringUtil.checkStrIsNotNull(nameAndPhoto[1])) {
					letter.setFriendPhoto(relPhotoPath + nameAndPhoto[1]);
				}
			}
		}
		this.friendUserId = friendUserId;
	}

	/**
	 * 
	 * 功能：查看私信列表
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-26
	 * 
	 * @return
	 */
	public String view() {

		return DEFAULT;
	}

	public String loadLetters() {

		privateLetters = privateLetterService.loadPrivateLetters(getUser()
				.getUserId());
		// 拆分好友昵称及头像
		if (privateLetters != null && privateLetters.size() > 0) {
			String relPhotoPath = PropertyUtil.getValue("PIC_SERVER");
			for (PrivateLetter letter : privateLetters) {
				String[] nameAndPhoto = letter.getFriendPetNameAndPhoto()
						.split("####");
				letter.setFriendPetName(nameAndPhoto[0]);
				if (nameAndPhoto.length == 2
						&& StringUtil.checkStrIsNotNull(nameAndPhoto[1])) {
					letter.setFriendPhoto(relPhotoPath + nameAndPhoto[1]);
				}
			}
		}
		request.getSession().setAttribute("MY_PRI_SESS_LETS",String.valueOf(privateLetters.size()));
		return DEFAULT;
	}
	
	/**
	 * 
	 * 功能：我的私信页面中私信数
	 * @author: zhijian.zhang
	 * @Date: 2012-1-8
	 *
	 * @return
	 * @throws Exception
	 */
	public String getPriNum() throws Exception{
		response.setContentType("text/html");
		response.getWriter().append((String)request.getSession().getAttribute("MY_PRI_SESS_LETS"));
		return null;
	}

	/**
	 * 
	 * 功能：加载好友详细私人信息列表
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-26
	 * 
	 * @return
	 */
	public String detail() {
		friendUser = sysUserService.loadSysUserById(friendUserId);
		privateLetters = privateLetterService.loadPrivateLetterDetails(
				getUser().getUserId(), friendUserId);
		return DEFAULT;
	}

	/**
	 * 
	 * 功能：删除私信功能
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-26
	 * 
	 * @return
	 */
	public String deleteLetter() {
		privateLetterService.deletePrivateLetter(letterId, getUser());
		return null;
	}

	/**
	 * 
	 * 功能：发送私信功能
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-26
	 * 
	 * @return
	 */
	public String sendLetter() throws Exception {
		response.setContentType("text/html");
		PrivateLetter privateLetter = privateLetterService.sendPrivateLetter(
				getUser().getUserId(), friendUserId, content);
		String result = "{contents:'(1)',time:'(2)',id:'(3)'}";
		result = result
				.replace("(1)", privateLetter.getContents())
				.replace(
						"(2)",
						DateFormater.formatDateAndTime(privateLetter
								.getSendTime()))
				.replace("(3)", String.valueOf(privateLetter.getId()));
		PrintWriter writer = response.getWriter();
		writer.append(result);
		writer.flush();
		writer.close();
		return null;
	}

	/**
	 * 
	 * 功能：删除与该好友的整个私信
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-29
	 * 
	 * @return
	 */
	public String deleteFriendLetters() {
		privateLetterService.deletePrivateLetters(getUser().getUserId(),
				friendUserId);
		return null;
	}

	/**
	 * 
	 * 功能：获取未打开私信数量
	 * 
	 * @author: zhijian.zhang
	 * @Date: 2011-12-30
	 * 
	 * @return
	 */
	public String getPriLetNum() throws Exception {
		response.setContentType("text/html");
		response.getWriter().append(
				String.valueOf(privateLetterService.getNoViewCounts(getUser()
						.getUserId())));
		
		return null;
	}
	
	public String goSendView() throws Exception {
		
		return dispatcher("/jsp/fg/letter/send_letter.jsp");
	}
}
